package sites.manager.common.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import sites.manager.admin.site.SiteService;
import sites.manager.common.kit.SiteInfo;
import sites.manager.common.kit.StrKit;
import sites.manager.common.model.Site;

/**
 * 项目启动后立即执行
 * 用于特定参数初始化
 */
@Slf4j
@Component
public class SiteApplicationRunner implements ApplicationRunner {

    @Value("${site.siteSign}")
    private String siteSign;

    @Autowired
    private SiteService siteService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Site site = null;
        if(StrKit.notBlank(siteSign)){
            //配置文件中指定了值
            site = siteService.getBySiteSign(siteSign);
            if(site != null){
                log.info("使用配置文件中的siteSign初始化系统参数");
            } else {
                site = siteService.getMainSite();
            }
        } else {
            //配置文件中没有指定值
            site = siteService.getMainSite();
        }

        initSiteInfo(site);
    }

    /**
     * SiteInfo参数赋值
     *
     * @param site
     */
    private void initSiteInfo(Site site){

        if(site == null){
            log.warn("获取主站点信息失败！");
            return;
        }

        log.info("开始进行系统参数初始化");
        SiteInfo.siteId = site.getId();
        SiteInfo.siteName = site.getSiteName();
        SiteInfo.siteSign = site.getSiteSign();
        SiteInfo.siteUrl = site.getSiteUrl();
        SiteInfo.siteTitle = site.getSiteTitle();
        SiteInfo.siteKeys = site.getSiteKeys();
        SiteInfo.siteDes = site.getSiteDes();
        SiteInfo.imgMaxSize = site.getImgMaxSize();
        SiteInfo.fileMaxSize = site.getFileMaxSize();
        SiteInfo.videoMaxSize = site.getVideoMaxSize();
        SiteInfo.fileSuffix = site.getFileSuffix();
        SiteInfo.thumbnailSuffix = site.getImgSuffix();
        SiteInfo.videoSuffix = site.getVideoSuffix();
        SiteInfo.accessLog = site.getAccessLog();

    }

}
